package com.ruoyi.project.mobile.utils;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.ruoyi.project.jiaqin.viprecord.domain.JiaqinBuyvipRecord;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class PayUtil {
    /**
     * 支付签名 获取预支付ID 再次签名返回支付五大参数
     * @param m
     * @param ip
     * @param openId
     * @return
     * @throws Exception
     */
    public static Map<String,String> yuZhiFu(JiaqinBuyvipRecord v, String ip, String openId) throws Exception {
        BigDecimal fen = v.getMoney().multiply(new BigDecimal(100));
        //统一下单 11个参数 返回预支付Id
        Map<String,String> map = new HashMap<>();
        map.put("appid","wxc0574270723e1af8");//微信支付分配的公众账号ID（企业号corpid即为此appId）
        map.put("mch_id","1601030220");//微信支付分配的商户号
        map.put("nonce_str", WXPayUtil.generateNonceStr());//随机字符串，长度要求在32位以内。
        map.put("body","家禽会员充值");//商品简单描述
        map.put("out_trade_no", v.getRecordId());
//        map.put("total_fee",fen.intValue()+"");//金额 单位分
        map.put("total_fee","1");//金额 单位分
        map.put("spbill_create_ip",ip);
        map.put("notify_url","https://server.szyujingkeji.com/api/jq/user/payresult");
        map.put("trade_type", "JSAPI");
        map.put("sign_type","MD5");
        map.put("openid",openId);
        String sign = WXPayUtil.generateSignature(map,"803hqka71x4vi7xycdkugctyyx3pb911");
        map.put("sign",sign);
        WXPay wxPay = new WXPay(WxPayConfigRuImpl.getInstance());
        Map<String,String> premap = wxPay.unifiedOrder(map);
        System.out.println("预支付ID:  "+premap);
        if("SUCCESS".equals(premap.get("return_code"))&&"SUCCESS".equals(premap.get("result_code"))){
            String prepay_id = premap.get("prepay_id");
            //返回支付六大参数
            Map<String, String> payMap = new HashMap<>();
            payMap.put("appId", "wxc0574270723e1af8");
            String time = new Date().getTime()+"";
            payMap.put("timeStamp", time.substring(0,10));
            payMap.put("nonceStr", WXPayUtil.generateNonceStr());
            payMap.put("signType", "MD5");
            payMap.put("package", "prepay_id="+prepay_id);
            String paySign = WXPayUtil.generateSignature(payMap,"803hqka71x4vi7xycdkugctyyx3pb911");
            payMap.put("paySign", paySign);
            return payMap;
        }
        return null;
    }
}
